package com.interesting.client.UI.user;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.interesting.MainActivity;
import com.interesting.R;
import com.interesting.base.core.BaseActivity;
import com.interesting.base.http.HttpUtil;
import com.interesting.base.utils.Constant;
import com.interesting.client.UI.commonActivity.UIUtil;
import com.interesting.client.application.AppHolder;
import com.interesting.client.bean.User;
import com.interesting.client.support.crop.Crop;

/** 信息详情+评论列表 */
public class UserActivity extends BaseActivity {
	private ImageView usericon;
	private EditText pass, newpass, repeatpass;
	private TextView username;
	private Button changepass, exit;
	private Crop iconCrop;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_center_activity);
		getSupportActionBar().setCustomView(R.layout.text_actionbar_custom_view);
		setTitle(getResources().getString(R.string.usercenter));
		usericon = (ImageView) findViewById(R.id.user_center_icon);
		pass = (EditText) findViewById(R.id.user_center_edit_pass);
		newpass = (EditText) findViewById(R.id.user_center_edit_newpass);
		repeatpass = (EditText) findViewById(R.id.user_center_edit_repeatpass);
		username = (TextView) findViewById(R.id.user_center_text_username);
		changepass = (Button) findViewById(R.id.user_center_button_changepass);
		exit = (Button) findViewById(R.id.user_center_button_exit);
		iconCrop = new Crop(UserActivity.this);
		iconCrop.setImageView(usericon);

		usericon.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 显示
				iconCrop.showDialog();
			}
		});
		if (AppHolder.getInsatnce().user != null && !AppHolder.getInsatnce().user.getUserId().equals("")) {
			username.setText(AppHolder.getInsatnce().user.getUserName());
			AppHolder.getInsatnce().imageLoader.DisplayImage(
					Constant.IMGPATH + "/" + AppHolder.getInsatnce().user.getUserIcon(), usericon,
					R.drawable.ic_big_bear);
		}
		changepass.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 修改密码
				String password = "";
				String newpassword = "";
				String repeatpassword = "";
				password = pass.getText().toString().trim();
				newpassword = newpass.getText().toString().trim();
				repeatpassword = repeatpass.getText().toString().trim();
				if (password.equals("") || newpassword.equals("") || repeatpassword.equals("")) {
					UIUtil.showToast(UserActivity.this, "修改密码失败,信息不完整");
				} else {
					UIUtil.showWaitDialog(getSupportFragmentManager());
					new ChagePassAsync().execute(password, repeatpassword);
				}
			}
		});
		exit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 退出
				AppHolder.getInsatnce().user = new User();
				Intent toMenu = new Intent(UserActivity.this, MainActivity.class);
				UserActivity.this.setResult(10, toMenu);
				UserActivity.this.finish();
			}
		});
	}

	/** 获取评论列表 */
	class ChagePassAsync extends AsyncTask<String, Void, JSONObject> {
		@Override
		protected JSONObject doInBackground(String... params) {
			Map<String, String> request = new HashMap<String, String>();
			request.put("act", "updateUserPwd");
			request.put("userId", AppHolder.getInsatnce().user.getUserId());
			request.put("oldpwd", params[0]);
			request.put("password", params[1]);
			return HttpUtil.parseResult(HttpUtil.post(request));
		}

		@Override
		protected void onPostExecute(JSONObject result) {
			if (result != null) {
				UIUtil.hideDialog(getSupportFragmentManager());
				UIUtil.showToast(UserActivity.this, "修改密码成功");
			} else {
				UIUtil.showToast(UserActivity.this, "修改密码失败");
			}
			super.onPostExecute(result);
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == Activity.RESULT_OK) {
			switch (requestCode) {
			case 1:
				Log.d("login", "1");
				iconCrop.onActivityResult(requestCode, resultCode, data);
				break;
			case 2:// 赋图片
				Log.d("login", "2");
				iconCrop.onActivityResult(requestCode, resultCode, data);
				new IconChangeAsync().execute(((BitmapDrawable) usericon.getDrawable()).getBitmap());
				break;
			case 3:// 裁剪
				Log.d("login", "3");
				iconCrop.onActivityResult(requestCode, resultCode, data);
				break;
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	class IconChangeAsync extends AsyncTask<Object, Void, String> {

		@Override
		protected String doInBackground(Object... params) {
			Map<String, Object> request = new HashMap<String, Object>();
			request.put("act", "updateUserIcon");
			request.put("userId", AppHolder.getInsatnce().user.getUserId());
			request.put("photo", params[0]);

			return HttpUtil.postImage(request);
		}

		@Override
		protected void onPostExecute(String result) {
			JSONObject response = HttpUtil.parseResult(result);
			if (response != null) {
				UIUtil.showToast(UserActivity.this, "头像上传成功");
				try {
					AppHolder.getInsatnce().user.setUserIcon(response.getString("photo"));
				} catch (JSONException e) {
					e.printStackTrace();
				}
			} else {
				UIUtil.showToast(UserActivity.this, "头像上传失败");
			}
			super.onPostExecute(result);
		}
	}

	@Override
	public void onBackPressed() {
		Intent toMenu = new Intent(UserActivity.this, MainActivity.class);
		UserActivity.this.setResult(10, toMenu);
		UserActivity.this.finish();
		super.onBackPressed();
	}
	
	
}
